package com.effective;

@SuppressWarnings("unused")
public class HashTable implements Cloneable {
	private Entry[] buckets;

	private static class Entry {
		final Object key;
		Object value;
		Entry next;

		Entry(Object key, Object value, Entry next) {
			this.key = key;
			this.value = value;
			this.next = next;
		}

		Entry deepCopy() {
			//return new Entry(key, value, next == null ? null : next.deepCopy());
			Entry result = new Entry(key, value, next);
			for(Entry entry = result; entry.next != null; entry = entry.next) {
				result.next =  new Entry(entry.next.key, entry.next.value, entry.next.next);
			}
			return result;
		}
	}

	@Override
	public HashTable clone() {
		try {
			HashTable result = (HashTable) super.clone();
			result.buckets = new Entry[buckets.length];
			for(int i = 0; i < buckets.length; i++) {
				result.buckets[i] = buckets[i].deepCopy();
			}
			return result;
		} catch (CloneNotSupportedException e) {
			throw new AssertionError();
		}
	}
}
